package lin.an.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lin.an.food.entity.FoodFavorite;
import lin.an.food.entity.FoodLike;
import lin.an.food.mapper.FoodFavoriteMapper;
import lin.an.food.param.FoodFavoriteParam;
import lin.an.food.service.FoodDetailsService;
import lin.an.food.service.FoodFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhoulinq
 * @date 2025/4/10
 */
@Service
public class FoodFavoriteServiceImpl extends ServiceImpl<FoodFavoriteMapper, FoodFavorite> implements FoodFavoriteService {

    @Override
    public boolean isFavorited(int foodId, int userId) {
        LambdaQueryWrapper<FoodFavorite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FoodFavorite::getFoodId, foodId)
                .eq(FoodFavorite::getUserId, userId);
        return this.count(queryWrapper) > 0;
    }

    @Override
    public void removeFavorite(int foodId, int userId) {
        // 1. 从food_favorite表中删除收藏记录
        LambdaQueryWrapper<FoodFavorite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FoodFavorite::getFoodId, foodId)
                .eq(FoodFavorite::getUserId, userId);
        this.remove(queryWrapper) ;
    }

    @Override
    public void addFavorite(int foodId, int userId) {
        FoodFavorite foodfavorite = new FoodFavorite();
        foodfavorite.setFoodId(foodId);
        foodfavorite.setUserId(userId);
        this.save(foodfavorite);
    }

    /**
     *根据用户id获取所有的美食id
     */
    @Override
    public List<Integer> getFoodId(int loginId) {
        LambdaQueryWrapper<FoodFavorite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FoodFavorite::getUserId,loginId);
        return this.listObjs(queryWrapper, o -> Integer.parseInt(o.toString()));
    }
}